package it.eng.area118.mdogpscollector.command;

import java.io.Serializable;

import it.eng.area118.mdocommon.android.util.StringUtils;

public class SearchFilter implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 779917461790573306L;

	private String name;

	private String km;

	private boolean openKmRange;

	private String direction;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getKm() {
		return km;
	}

	public void setKm(String km) {
		this.km = km;
	}

	public boolean isOpenKmRange() {
		return openKmRange;
	}

	public void setOpenKmRange(boolean openKmRange) {
		this.openKmRange = openKmRange;
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public String getSearchKey() {
		StringBuffer buffer = new StringBuffer();
		int currLen = 0;
		currLen = addToBuffer(buffer, name, currLen, false);
		currLen = addToBuffer(buffer, km, currLen, true);
		currLen = addToBuffer(buffer, direction, currLen, true);
		return buffer.toString();
	}

	private int addToBuffer(StringBuffer buffer, String field, int curr, boolean isNotFirst) {
		if (StringUtils.isEmpty(field)) {
			return curr;
		}
		if (isNotFirst) {
			buffer.append("-");
		}
		buffer.append(StringUtils.defaultIfEmpty(field, ""));

		return buffer.length();
	}

	@Override
	public String toString() {
		return "SearchFilter [name=" + name + ", km=" + km + ", openKmRange=" + openKmRange + "]";
	}

}
